package com.zxing;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxing.permission.IPermissionsChecker;
import com.zxing.permission.PermissionsChecker;
import com.zxinglibrary.zxing.activity.CaptureActivity;

/**
 * 作者： njb
 * 时间： 2018/5/22 0022-上午 8:28
 * 描述：
 * 来源：
 */
public class MainActivity extends AppCompatActivity {

    TextView titleTv;

    ImageView titleRightIv;

    Toolbar toolbar;

    Button qrCode;
    private IPermissionsChecker permissionsChecker;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        init();
    }

    private void init() {
        titleTv = findViewById(R.id.titleTv);
        titleRightIv = findViewById(R.id.title_right_iv);
        toolbar = findViewById(R.id.toolbar);
        qrCode = findViewById(R.id.btn_qrcode);
        qrCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CaptureActivity.class));
            }
        });
    }

    private void checkPermission() {
        String[] permission = {
                Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        };
        permissionsChecker = new PermissionsChecker(this);
        if (null != (permission = permissionsChecker.lacksPermissions(permission))) {
            permissionsChecker.requestPermissions(new PermissionsChecker.OnPermissionsResultListener() {
                @Override
                public void onGranted() {
                }

                @Override
                public void onDenied() {
                    permissionsChecker.showMissingPermissionDialog(
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dialogInterface.dismiss();
                                }
                            });
                }
            }, this, permission);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (null != permissionsChecker) {
            permissionsChecker.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


}
